﻿//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2012
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI.HtmlControls;
using DotNetNuke.Framework;
using DotNetNuke.Modules.Contest.Components.Entities;
using DotNetNuke.Services.Localization;

namespace DotNetNuke.Modules.Contest.Components.Common
{
    public class Utils
    {

        #region Friendly Display

        /// <summary>
        /// 
        /// </summary>
        /// <param name="numerator"></param>
        /// <param name="denominator"></param>
        /// <returns></returns>
        public static string CalucalatePercentForDisplay(int numerator, int denominator)
        {
            if (denominator != 0)
            {
                return (int)(((double)numerator / denominator) * 100) + @" %";
            }
            return "0 %";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string CalculateDateForDisplay(DateTime date)
        {
            var utcDate = date.ToUniversalTime();
            var utcTimeDifference = Services.SystemDateTime.SystemDateTime.GetCurrentTimeUtc() - utcDate;

            if (utcTimeDifference.TotalSeconds < 60)
            {
                return (int)utcTimeDifference.TotalSeconds + Localization.GetString("secondsago", Constants.SharedResourceFileName);
            }
            if (utcTimeDifference.TotalMinutes < 60)
            {
                if (utcTimeDifference.TotalMinutes < 2)
                {
                    return (int)utcTimeDifference.TotalMinutes + Localization.GetString("minuteago", Constants.SharedResourceFileName);
                }
                return (int)utcTimeDifference.TotalMinutes + Localization.GetString("minutesago", Constants.SharedResourceFileName);
            }
            if (utcTimeDifference.TotalHours < 24)
            {
                if (utcTimeDifference.TotalHours < 2)
                {
                    return (int)utcTimeDifference.TotalHours + Localization.GetString("hourago", Constants.SharedResourceFileName);
                }
                return (int)utcTimeDifference.TotalHours + Localization.GetString("hoursago", Constants.SharedResourceFileName);
            }

            if (utcTimeDifference.TotalDays < 7)
            {
                if (utcTimeDifference.TotalDays < 2)
                {
                    return (int)utcTimeDifference.TotalDays + Localization.GetString("dayago", Constants.SharedResourceFileName);
                }
                return (int)utcTimeDifference.TotalDays + Localization.GetString("daysago", Constants.SharedResourceFileName);
            }

            if (utcTimeDifference.TotalDays < 30)
            {
                if (utcTimeDifference.TotalDays < 14)
                {
                    return (int)utcTimeDifference.TotalDays / 7 + Localization.GetString("weekago", Constants.SharedResourceFileName);
                }
                return (int)utcTimeDifference.TotalDays / 7 + Localization.GetString("weeksago", Constants.SharedResourceFileName);
            }

            if (utcTimeDifference.TotalDays < 180)
            {
                if (utcTimeDifference.TotalDays < 60)
                {
                    return (int)utcTimeDifference.TotalDays / 30 + Localization.GetString("monthago", Constants.SharedResourceFileName);
                }
                return (int)utcTimeDifference.TotalDays / 30 + Localization.GetString("monthsago", Constants.SharedResourceFileName);
            }

            //if (utcTimeDifference.TotalDays < 60)
            //{
            //    return 1 + Localization.GetString("monthago", Constants.SharedResourceFileName);
            //}

            // anything else (this is the only time we have to personalize it to the user)
            return date.ToShortDateString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string CalculateCountForDisplay(int count)
        {
            var friendlyCount = count.ToString();

            if (count < 999)
            {
                return friendlyCount;
            }
            if (count < 9999)
            {
                return friendlyCount.Substring(0, 1) + "K";
            }
            if (count < 99999)
            {
                return friendlyCount.Substring(0, 2) + "K";
            }
            if (count < 999999)
            {
                return friendlyCount.Substring(0, 3) + "K";
            }
            if (count < 9999999)
            {
                return friendlyCount.Substring(0, 1) + "M";
            }
            if (count < 99999999)
            {
                return friendlyCount.Substring(0, 2) + "M";
            }
            return friendlyCount;
        }

        /// <summary>
        /// Get a substring of the first N characters.
        /// </summary>
        /// <remarks></remarks>
        public static string TruncateString(string source, int length, bool showElipse)
        {
            if (source.Length > length)
            {
                source = source.Substring(0, length);
                if (showElipse)
                {
                    source += @"...";
                }
            }
            return source;
        }

        /// <summary>
        /// This method is meant to update SEO related meta information in the DotNetNuke base page. 
        /// </summary>
        /// <param name="defaultPage"></param>
        /// <param name="objEntry"></param>
        /// <param name="moduleContext"></param>
        public static void SetPageMeta(CDefault defaultPage, EntryInfo objEntry, UI.Modules.ModuleInstanceContext moduleContext)
        {
            var title = TruncateString(Localization.GetString("PageTitlePrefix", Constants.SharedResourceFileName) + objEntry.EntryName, Constants.SeoTitleLimit, false);
            defaultPage.Title = title;

            var meta = new HtmlMeta();
            meta.Attributes.Add("property", "og:title");
            meta.Attributes.Add("content", Localization.GetString("PageTitlePrefix", Constants.SharedResourceFileName) + objEntry.EntryName);
            defaultPage.Header.Controls.Add(meta);

            var description = TruncateString(objEntry.Description, Constants.SeoDescriptionLimit, false);

            if (description != null)
            {
                defaultPage.Description = description;

                meta = new HtmlMeta();
                meta.Attributes.Add("property", "og:description");
                meta.Attributes.Add("content", description);
                defaultPage.Header.Controls.Add(meta);
            }

            //var keyWords = "DotNetNuke";
            //var keyCount = 1;
            //var count = keyCount;

            //foreach (var term in objProject.Terms.TakeWhile(term => count <= Constants.SeoKeywordsLimit))
            //{
            //    keyWords += "," + term.Name;
            //    keyCount += 1;
            //}

            //if (keyWords != null)
            //{
            //    defaultPage.KeyWords = keyWords;
            //}

            meta = new HtmlMeta();
            meta.Attributes.Add("property", "og:type");
            meta.Attributes.Add("content", "product");
            defaultPage.Header.Controls.Add(meta);

            var link = moduleContext.NavigateUrl(moduleContext.TabId, "", false, "view=detail",
                                                                       "entry=" + objEntry.ContestEntryId);
            meta = new HtmlMeta();
            meta.Attributes.Add("property", "og:url");
            meta.Attributes.Add("content", link);
            defaultPage.Header.Controls.Add(meta);

            meta = new HtmlMeta();
            meta.Attributes.Add("property", "og:site_name");
            meta.Attributes.Add("content", moduleContext.PortalSettings.PortalName);
            defaultPage.Header.Controls.Add(meta);

            var siteDir = moduleContext.PortalSettings.HomeDirectory;
            var firstCharacter = siteDir.IndexOf("/Portals/");
            siteDir = siteDir.Remove(0, firstCharacter);

            meta = new HtmlMeta();
            meta.Attributes.Add("property", "og:image");

            meta.Attributes.Add("content", "http://" + moduleContext.PortalAlias.HTTPAlias + siteDir + objEntry.SmallImageUrl);
            defaultPage.Header.Controls.Add(meta);
        }

        #endregion

    }
}